package main

import (
	"fmt"
	"github.com/sentiger/ftcp/Iface"
	"github.com/sentiger/ftcp/Net"
)

//ping test 自定义路由
type PingRouter struct {
	Net.BaseRouter //一定要先基础BaseRouter
}

//Test PreHandle
func (this *PingRouter) PreHandle(request Iface.IRequest) {
	fmt.Println("Call Router PreHandle")
}

//Test Handle
func (this *PingRouter) Handle(request Iface.IRequest) {
	fmt.Println("客户端发送数据", string(request.GetData()))
	err := request.GetConnection().SendMsg(request.GetMsgId(), []byte("ping...ping...ping\n"))
	if err != nil {
		fmt.Println("call back ping ping ping error")
	}
}

//Test PostHandle
func (this *PingRouter) PostHandle(request Iface.IRequest) {
	fmt.Println("Call Router PostHandle")
}

func main() {
	s := Net.NewServer("sentiger")
	s.SetOnConnStart(func(conn Iface.IConnection) {
		conn.SendMsg(1, []byte("连接成功"))
	})
	s.SetOnConnStop(func(conn Iface.IConnection) {
		fmt.Println("连接关闭了", conn)
	})
	s.AddRouter(0, &PingRouter{})
	s.Serve()
}
